const express = require('express')
const router = express.Router()
const db = require('../database/index')
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })
//发表 局部中间件 根据数据库的字段进行发布
router.post('/add', upload.single('cover_img'), (req, res) => {
    // 文件上传   下载中间件multer  
    // console.log(req.file);
    // console.log(req.body);
    let { title, cate_id, content, state } = req.body
    let cover_img = '/uploads/'+req.file.filename
    let author_id = req.user.id
    let pub_date = '1111-11-11 11:11:11'
    let is_delete = 0

    let sql = `insert into article set title="${title}" , cate_id=${cate_id} ,content="${content}", state="${state}" ,cover_img="${cover_img}", author_id=${author_id} ,pub_date="${pub_date}", is_delete="${is_delete}"  `
    db(sql,(err,results)=>{
        if(err)throw err
        if (results.affectedRows !== 1) {
            return res.send({
                status:1,
                message:'发布失败！'

            })
        }
        res.send({
            status:0,
            message:'发布文章成功！'
        })
    })
})












module.exports = router